package com.qing.demo.netty.demo1.nio.server;

import com.qing.demo.netty.demo1.nio.ChannelAdapter;
import com.qing.demo.netty.demo1.nio.ChannelHandler;

import java.io.IOException;
import java.nio.channels.Selector;
import java.nio.charset.Charset;

/**
 * @author caozhipeng
 * @date 2025/7/22
 */
public class NioServerHandler extends ChannelAdapter {

    public NioServerHandler(Selector selector, Charset charset) {
        super(selector, charset);
    }

    @Override
    public void channelActive(ChannelHandler channelHandler) {
        try {
            System.out.println("服务端连接成功! IP:"+channelHandler.channel().getLocalAddress());
            channelHandler.writeAndFlush("服务端连接成功!\r\n");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }

    @Override
    public void channelRead(ChannelHandler channelHandler, Object msg) {
        System.out.println("服务端接收消息: "+msg);
        channelHandler.writeAndFlush("服务端处理成功！\r\n");
    }
}
